<template>
	<main-content
		:tableHeight.sync="tableHeight"
		:otherDependenComponents="otherDependenComponents"
	>
		<template slot="query">
			<el-form
				ref="formRef"
				:inline="true"
				:model="queryData"
				class="flex-warp-container"
			>
				<!-- <el-form-item
					:label="$t('common.belong_merchant') + ':'"
					class="flex-row-container"
					label-width="77px"
				>
					<merchant-select
						v-model="queryData.merchantId"
						:merchantInfo.sync="merchantObj"
					></merchant-select>
				</el-form-item> -->
				<el-form-item
					label="统计日期:"
					class="flex-row-container"
					label-width="101px"
				>
					<el-date-picker
						v-model="searchTime"
						size="medium"
						:picker-options="pickerShortcutWithCurrPeriod"
						type="daterange"
						range-separator="-"
						:start-placeholder="$t('report.start_date')"
						:end-placeholder="$t('report.end_date')"
						:clearable="false"
					></el-date-picker>
				</el-form-item>
				<el-form-item
					:label="$t('report.general_account') + ':'"
					class="flex-row-container"
					label-width="77px"
				>
					<el-input
						v-model.trim="queryData.topProxyName"
						clearable
						:placeholder="$t('common.please_enter')"
						v-input="{ name: 'num_alp' }"
					></el-input>
				</el-form-item>
				<el-form-item
					label="团队排行:"
					class="proxyItem flex-row-container"
					label-width="77px"
				>
					<el-select
						v-model="queryData.teamType"
						class="proxyInput"
						:placeholder="$t('common.select_all')"
						size="medium"
					>
						<el-option
							v-for="item in profitRankList"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button
						type="primary"
						icon="el-icon-search"
						size="medium"
						:disabled="loading"
						@click="search"
					>
						{{ $t('common.search') }}
					</el-button>
					<el-button
						icon="el-icon-refresh-left"
						size="medium"
						:disabled="loading"
						@click="reset"
					>
						{{ $t('common.reset') }}
					</el-button>
					<el-button
						v-if="hasPermission('90330052')"
						type="warning"
						icon="el-icon-download"
						size="medium"
						:disabled="loading"
						:loading="exportLoading"
						@click="handleExportExcel"
					>
						{{ $t('common.export') }}
					</el-button>
					<el-button
						type="success"
						icon="el-icon-sort"
						:disabled="loading"
						size="medium"
						@click="openColSettings"
					>
						{{ $t('common.row_set') }}
					</el-button>
				</el-form-item>
				<date-picker-shortcuts
					kind="shortcutForButtons"
					:action="handleShortcutBtnClick"
					:loading="loading"
				/>
			</el-form>
			<el-link :underline="false" type="danger">
				{{ $t('report.refresh_warning_text') }}
			</el-link>
		</template>
		<template slot="content">
			<el-table
				key="father"
				v-loading="loading"
				element-loading-spinner="el-icon-loading"
				border
				ref="elTable"
				size="mini"
				class="small-size-table"
				:data="tableData"
				style="width: 100%"
				show-summary
				@sort-change="changeTableSort"
				:max-height="tableHeight"
				highlight-current-row
				:summary-method="
					(val) => handleRow(val, subSummary, totalSummary)
				"
				:header-cell-style="getRowClass"
			>
				<el-table-column
					prop="topProxyRank"
					align="center"
					label="序号"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.topProxyRank || '-' }}
					</template>
				</el-table-column>
				<el-table-column
					prop="topProxyName"
					align="center"
					label="总代账号"
					show-overflow-tooltip
					v-if="tableColumnData['总代账号']"
				>
					<template slot-scope="scope">
						{{ scope.row.topProxyName || '-' }}
					</template>
				</el-table-column>
				<el-table-column
					prop="teamWithdrawable"
					align="center"
					label="团队可提现额"
					show-overflow-tooltip
					v-if="tableColumnData['团队可提现额']"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'USDT',
									scope.row.teamWithdrawable
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					prop="teamReturnLoan"
					align="center"
					label="团队应还账款"
					show-overflow-tooltip
					v-if="tableColumnData['团队应还账款']"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'USDT',
									scope.row.teamReturnLoan
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					prop="validBetAmount"
					align="center"
					label="总有效投注"
					show-overflow-tooltip
					v-if="tableColumnData['总有效投注']"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'USDT',
									scope.row.validBetAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					prop="directlyMemberValidBetAmount"
					align="center"
					label="直属会员有效投注"
					show-overflow-tooltip
					v-if="tableColumnData['直属会员有效投注']"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'USDT',
									scope.row.directlyMemberValidBetAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					prop="teamRebateAmount"
					align="center"
					label="团队返点"
					show-overflow-tooltip
					v-if="tableColumnData['团队返点']"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'USDT',
									scope.row.teamRebateAmount
								)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					prop="netAmount"
					align="center"
					label="投注盈亏"
					show-overflow-tooltip
					v-if="tableColumnData['投注盈亏']"
				>
					<template slot-scope="scope">
						<p :style="handleNumberColor(scope.row.netAmount)">
							{{ handleTotalNumber('USDT', scope.row.netAmount) }}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					prop="creditQuota"
					align="center"
					label="总授信额度"
					show-overflow-tooltip
					v-if="tableColumnData['总授信额度']"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber('USDT', scope.row.creditQuota)
							}}
						</p>
					</template>
				</el-table-column>
				<el-table-column
					prop="creditBalance"
					align="center"
					label="可用额度"
					show-overflow-tooltip
					v-if="tableColumnData['可用额度']"
				>
					<template slot-scope="scope">
						<p>
							{{
								handleTotalNumber(
									'USDT',
									scope.row.creditBalance
								)
							}}
						</p>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<el-pagination
				v-show="!!total"
				class="pageValue"
				:current-page.sync="pageNum"
				background
				layout="total, sizes,prev, pager, next, jumper"
				:page-size.sync="pageSize"
				:page-sizes="pageSizes"
				:total="total"
				:pager-count="9"
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
			></el-pagination>
		</template>
		<el-dialog
			:title="$t('common.row_set')"
			center
			:visible.sync="colSettings.visible"
			width="475px"
			class="setting-div"
		>
			<div>
				<el-link type="primary" class="mb15" @click="clickDel">
					复原缺省
				</el-link>
			</div>
			<div
				v-for="(value, name1) in activityListField"
				:key="name1"
				class="setting-div"
			>
				<el-checkbox v-model="dialogColumnData[name1]">
					{{ name1 }}
				</el-checkbox>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="colSettings.visible = false">
					{{ $t('common.cancel') }}
				</el-button>
				<el-button type="primary" @click="handleOrderList">
					{{ $t('common.submit') }}
				</el-button>
			</div>
		</el-dialog>
	</main-content>
</template>

<script>
import list from '@/mixins/list'
import dayjs from 'dayjs'
import { routerNames } from '@/utils/consts'
import summaryList from './components/summaryList'
import MainContent from '@/components/MainContent/index.vue'
import DatePickerShortcuts from '@/components/DatePickerShortcuts'
const setSearchTime = () => [
	dayjs()
		.subtract(0, 'day')
		.startOf('day'),
	dayjs()
		.subtract(0, 'day')
		.endOf('day')
]
export default {
	name: routerNames.agentAndLossRanking,
	components: {
		MainContent,
		DatePickerShortcuts
	},
	mixins: [list, summaryList],
	data() {
		return {
			tableHeight: 480,
			otherDependenComponents: [],
			exportLoading: false,
			queryData: {
				teamType: '1'
			},
			searchTime: setSearchTime(),
			tableData: [],
			subSummary: {},
			totalSummary: {},
			merchantObj: '',
			axiosDate: '',
			colSettings: {
				visible: false,
				dialogColumnData: {}
			},
			activityListField: {
				总代账号: true,
				团队可提现额: true,
				团队应还账款: true,
				总有效投注: true,
				直属会员有效投注: true,
				团队返点: true,
				投注盈亏: true,
				总授信额度: true,
				可用额度: true
			},
			profitRankList: [
				{
					label: '应还账款排行',
					value: '1'
				},
				{
					label: '可提现额排行',
					value: '2'
				}
			]
		}
	},
	computed: {},
	watch: {},
	created() {
		this.initIndexDB('commissionReport', this.activityListField)
	},
	mounted() {},
	methods: {
		handleShortcutBtnClick(val) {
			this.searchTime = val
			this.search()
		},
		// 提交列设置数据
		handleOrderList() {
			this.colSettings.visible = false
			this.updateIndexDB()
		},
		// 打开列设置弹框
		openColSettings() {
			this.getIndexDB(this.activityListField)
			this.colSettings.visible = true
		},
		clickDel() {
			this.dialogColumnData = {}
			this.dialogColumnData = {
				总代账号: true,
				团队可提现额: true,
				团队应还账款: true,
				总有效投注: true,
				直属会员有效投注: true,
				团队返点: true,
				投注盈亏: true,
				总授信额度: true,
				可用额度: true
			}
		},
		changeTableSort({ column, prop, order }) {
			this.queryData.orderKey = prop
			if (order === 'ascending') {
				// 升序
				this.queryData.orderType = 'asc'
			} else if (column.order === 'descending') {
				// 降序
				this.queryData.orderType = 'desc'
			} else {
				delete this.queryData.orderKey
				delete this.queryData.orderType
			}
			this.loadData()
		},
		loadData() {
			if (+new Date() - 500 < this.axiosDate) return
			const timeArr = this.searchTime || []
			let params = {
				...this.queryData,
				reportStDate: dayjs(timeArr[0])
					.startOf('day')
					.format('YYYYMMDD'),
				reportEdDate: dayjs(timeArr[1])
					.endOf('day')
					.format('YYYYMMDD')
			}
			params = this.getParams(params)
			params.teamType = Number(params.teamType)
			this.tableData = []
			this.loading = true
			this.pageCurrency = this.merchantObj?.currency || ''
			this.$api
				.getAgentAndLossRankingPage(params)
				.then((res) => {
					this.loading = false
					this.axiosDate = +new Date()
					this.tableData = res?.data?.record || []
					this.total = res?.data?.totalRecord || 0
					this.subSummary = res?.data?.summary || {}
				})
				.catch(() => {
					this.loading = false
				})
			this.$api
				.getAgentAndLossRankingTotal(params)
				.then((res) => {
					this.loading = false
					this.totalSummary = res?.data || {}
				})
				.catch(() => {
					this.loading = false
				})
		},
		search() {
			this.pageNum = 1
			this.loadData()
		},
		reset() {
			this.pageNum = 1
			this.queryData = {
				teamType: '1'
			}
			this.merchantObj = ''
			this.searchTime = setSearchTime()
			if (this.$refs.elTable) {
				this.$refs.elTable.clearSort()
			}
			this.loadData()
		},
		handleExportExcel() {
			const timeArr = this.searchTime || []
			const params = {
				...this.queryData,
				reportStDate: dayjs(timeArr[0])
					.startOf('day')
					.format('YYYYMMDD'),
				reportEdDate: dayjs(timeArr[1])
					.endOf('day')
					.format('YYYYMMDD')
			}
			this.exportLoading = true
			this.exportExcelPublic({
				api: 'getAgentAndLossRankingExport',
				params
			}).finally(() => {
				this.exportLoading = false
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.tips-box {
	position: absolute;
	right: 10px;
	top: 50%;
	margin-top: -12px;
}
::v-deep .el-dialog__header {
	color: #909399;
	font-weight: 700;
}
::v-deep .el-table__fixed-footer-wrapper .cell,
::v-deep .el-table__footer-wrapper .cell {
	height: 100%;
	padding: 0 !important;
	margin: 0 !important;
	> div {
		height: 100%;
	}
}
::v-deep .footer_count_row {
	min-height: 50px;
	height: 50%;
	display: flex;
	font-weight: 700;
	padding: 5px 0;
	align-items: center;
	flex-direction: column;
	justify-content: center;
}
::v-deep .footer_count_row_border {
	border-top: 1px solid #ebeef5;
}
</style>
